#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>

sigjmp_buf buf;

void handler(int sig)
{
    static int counter = 1;

    if (counter++ < 5)
        alarm(1);
    else
        siglongjmp(buf, 1);
}

char *tfgets(char *s, int size, FILE *stream)
{
    char *pc;

    signal(SIGALRM, handler);

    alarm(1);

    if (!sigsetjmp(buf, SIGALRM))
    {
        pc = fgets(s, size, stream);
        return pc;
    }
    else
        return NULL;
}

int main()
{
    char s[256];

    printf("%s", tfgets(s, 256, stdin)?s:"NULL\n");
    return 0;
}
